<template>
  <div>
    <el-row class="table-head">
      <el-col :span="12">{{headTitle[0]}}</el-col>
      <el-col :span="12">{{headTitle[1]}}</el-col>
    </el-row>
    <div class="table-body">
      <el-tree
        :data="dataObject"
        :props="defaultProps"
        node-key="id"
        :highlight-current="true"
        :expand-on-click-node="false"
        :render-content="renderContent">
      </el-tree>
    </div>
  </div>
</template>

<script>
  let id = 1000;

  import fetch from '@/utils/fetch';

  export default {
    name: 'ymyTableTree',
    props: ['headTitle','dataObject'],
    data() {
      return {
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        selectedNode: {}
      };
    },
    methods: {
      /**
       * 添加子级别
       */
      append(store, data) {
        store.append({ id: id++, label: 'testtest', children: [] }, data);
      },
      /**
       * 删除选中节点
       */
      remove() {
        this.selectedNode.store.remove(this.selectedNode.data);
      },
      /**
       * 选中节点信息
       */
      selected(node, store, data){
        this.selectedNode = {
          data: data,
          store: store
        };
        this.$emit('selectednode', data);
      },

      renderContent(h, { node, data, store }) {
        return (
          <span on-click={ () => this.selected(node, store, data) } class="table-tr">
            <span class="table-button">
            <el-button size="mini" on-click={ () => this.append(store, data) }> + </el-button>
          </span>
          <span class="table-text">
            <span>{data.label}</span>
          <span>{data.id}</span>
          </span>
        </span>);
      }
    },
  };

</script>

<style lang="scss">
  .table-tree{
    width: 360px;
    .table-head{
      height: 28px;
      line-height: 28px;
      text-align: center;
      background-color: #eef1f6;
      border: 1px solid #d1dbe5;
      border-bottom: none;
      div:first-child{
        border-right: 1px solid #d1dbe5;
      }
    }
    .table-body .el-tree{
      border-bottom: none;
    }
    .el-tree-node__expand-icon{
      float: left;
      width: 7px;
      margin-top: 12px;
    }
    .el-tree-node__content{
      border-bottom: 1px solid #d1dbe5;
    }
    .table-tr{
      display: block;
      overflow: hidden;
      .table-button{
        float: right;
        width: 18px;
        margin-right: 12px;
      }
      .table-text{
        display: block;
        overflow: hidden;
        span:last-child{
          display: block;
          overflow: hidden;
        }
        span:first-child{
          float: right;
          width: 150px;
          padding-left: 10px;
          border-left: 1px solid #d1dbe5;
        }
      }
    }
  }
</style>>
